<?php

namespace App\Jobs\Injection;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\Entity\Asset\Vlive\Vchan;
use DB;
use Storage;

class VchanInjectionJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * 任务最大尝试次数
     *
     * @var int
     */
    public $tries = 3;

    /**
     * 任务运行的超时时间。
     *
     * @var int
     */
    public $timeout = 3600;

    /**
     * [$injectionWhere 注入的条件]
     * @var array
     */
    protected $injectionWhere = ['online_status' => 1, 'publish_status' => 0];

    /**
     * [$injectVchanHistory description]
     * @var [type]
     */
    protected $injectVchanHistory;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($injectVchanHistory)
    {
        $this->injectVchanHistory = $injectVchanHistory;
    }


    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 开始时间
        $starttime = microtime(true);
        // 开始使用内存
        $startMemory = memory_get_usage();

        /**
         * 生成xml, 对上线的媒资进行注入
         */
        // 生成xml
        $uploadConfig = config('custom.base.upload.xml');
        $driver       = $uploadConfig['driver'];
        $dir          = $uploadConfig['dir'];
        $rootDir      = config('filesystems.disks.' . $driver . '.upload');

        // 完整路径
        $fullPath = $rootDir . '/' . $dir . '/';

        // 注入媒资总数
        $injectionVchancount = 0;
        // 错误的行数
        $errorRow = [];

        /**
         * 配值
         */
        // ftp配置
        $ftp_config = config('ftp.connections.connection1');
        // 分块
        $injection_chunk_num = config('custom.base.job.vchan.injection_chunk_num');
        // cp_id
        $config_media_injection_cp_id    = config('custom.base.media_injection.cp_id');
        // url
        $config_media_injection_url    = config('custom.base.media_injection.url');

        /**
         * xml注入, 500条一次处理
         */
        Vchan::where($this->injectionWhere)->with(['vccat', 'vcareacats', 'vcposters', 'vcstreams', 'vepgs', 'sps', 'vccattags', 'vlangs'])->chunk($injection_chunk_num, function ($vchans) use ($driver, $fullPath, $ftp_config, $config_media_injection_cp_id, $config_media_injection_url, &$injectionVchancount, &$errorRow) {
            foreach ($vchans as $vchan) {
                // 完成数+1
                $this->injectVchanHistory->update([
                    'done_count' => DB::raw("done_count + 1")
                ]);
                /**
                 * 数据准备
                 */
                $vchan_id = $vchan->id;
                // 属性名:ElementType;取值:Series-连续剧剧头信息|Program-节目信息|Movie-媒体内容信息|Channel-频道信息|PhysicalChannel-频道物理参数信息|Schedule-频道节目编排信息|Cast-人物信息|Picture-图片信息|Category-编排目录信息;必填:Y;
                $elementType = 'Series';
                // 属性名:ID(唯一ID);必填:Y;类型长度:32;
                $id = 'HDTYZB' . str_pad($vchan_id, 26, "0", STR_PAD_LEFT);
                // 属性名:Action(操作类型);取值:发布=>REGIST, 更新=>UPDATE, 删除=>DELETE;必填:Y;
                $action = 'REGIST';
                // 属性名:Code(全局唯一标志);必填:Y;类型长度:128;
                $code = $id;
                // 属性名:BizDomain(业务类型);取值:0=>IPTV业务(默认), 1=>MOBILE业务, 2=>PC业务, 3=>IPTV和PC业务;必填:N;
                $bizDomain = 0;
                // 属性名:Priority(LSP要处理的优先级);取值:1-10(10最高);类型长度:int,3;必填:N;
                $priority = 1;
                // // 属性名:OnlineIdentify(第三方上线标识);必填:N;类型长度:string;自动上线规则配置;
                // if($vasseti->charge_type === 0){
                //     $onlineIdentify = config('custom.base.media_injection.free_flag');
                // } else {
                //     $onlineIdentify = config('custom.base.media_injection.charge_flag');
                // }
                // 节点名:Name(连续剧名称);必填:Y;类型长度:128;
                $name = $vchan->name;
                // 节点名:OrderNumber(节目订购编号);必填:N;类型长度:string,10;
                $orderNumber = '';
                // 节点名:OriginalName(原名);必填:N;类型长度:string,128;
                $originalName = '';
                // 节点名:SortName(剧集排序名称);必填:N;类型长度:string,128;
                $sortName = '';
                // 节点名:SearchName(索引名称);必填:N;类型长度:string,160;
                $searchName = '';
                // 节点名:OrgairDate(首播日期YYYYMMDD);必填:Y;类型长度:string,8;
                // $orgairDate = date("Ymd", strtotime($vasseti->firstdate));
                $orgairDate = '';
                // 节点名:LicensingWindowStart(有效开始时间YYYYMMDDHH24MiSS);必填:Y;类型长度:string,14;
                $licensingWindowStart = ''; // $vasseti->online_at;
                // 节点名:LicensingWindowEnd(有效结束时间YYYYMMDDHH24MiSS);必填:Y;类型长度:string,14;
                $licensingWindowEnd = ''; // $vasseti->offline_at;
                // 节点名:DisplayAsNew(新到天数);必填:N;类型长度:int,3;
                $displayAsNew = '';
                // 节点名:DisplayLastChance(剩余天数);必填:N;类型长度:int,3;
                $displayLastChance = '';
                // 节点名:Macrovision(拷贝保护标志);取值:0=>无拷贝保护,1=>有拷贝保护;必填:Y;类型长度:int,1;默认:0;
                $macrovision = 0;
                // 节点名:Description(节目描述);必填:Y;类型长度:string,2048;
                $description = $vchan->short_desc;
                // 节点名:PriceTaxIn(列表定价);必填:N;类型长度:float,12,2;
                $price = '';
                // 节点名:VolumnCount(总集数);必填:Y;类型长度:int,4;
                // // 如果是电视剧就是总集数【如果有分集就是电视剧】, 否则就是1
                // $volumnCount = 1;
                // if ($vasseti->vserialis) {
                //     $volumnCount = $vasseti->episode;
                // }
                // 节点名:Status(节目状态标志);必填:N;类型长度:int,1;默认:1;
                $status = 1;
                // 节点名:Type(节目内容类型);必填:Y;类型长度:int,2;字符串,表示内容类型
                $type = $vchan->vccat->name;
                // 节点名:Keywords(关键字);必填:Y;类型长度:string,1024;多个关键字使用;分隔;
                $keywords = '';
                // 节点名:Tags(标签集合);必填:Y;类型长度:string,1024;多个标签使用;分隔;
                $tags = '';
                if ($tags = $vchan->vccattags->pluck('name')) {
                    $tags = $tags->implode(';');
                }
                // 节点名:Reserve1(保留字段);必填:N;类型长度:string,1024;
                $reserve1 = '';
                // 节点名:Reserve2(保留字段);必填:N;类型长度:string,1024;
                $reserve2 = '';                
                // 节点名:Reserve3(保留字段);必填:N;类型长度:string,256;
                $reserve3 = '';                
                // 节点名:Reserve4(保留字段);必填:N;类型长度:string,256;
                $reserve4 = '';                
                // 节点名:Reserve5(保留字段);必填:N;类型长度:string,256;
                $reserve5 = '';
                // 节点名:RMediaCode(关联内容唯一标识);必填:N;类型长度:String,128;
                $rMediaCode = '';
                /**
                 * xml生成
                 */
                // 创建version标签$version=$doc->createElement("version");$versionTextNode=$doc->createTextNode("1.0");$version->appendchild($versionTextNode);当<root>标签中有值时就可以显示完整的<root>标签了
                // header("Content-type: text/xml"); //头文件非常重要
                // 创建xml文件, 建立DOM对象
                $dom = new \DOMDocument('1.0', 'utf-8');

                // 格式化输出，友好地保留缩进和空格
                // $doc->formatOutput = true;

                // 建立<ADI>元素
                $adi = $dom->createElement('ADI');
                // 添加属性
                $adi->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
                // 业务类型
                $adi->setAttribute('BizDomain',$bizDomain);
                // LSP要处理的优先级
                $adi->setAttribute('Priority', $priority);
                // 把<ADI>元素追加到文档里面
                $dom->appendChild($adi);

                // 建立<Objects>元素并将其作为<ADI>的子元素
                $objects = $dom->createElement('Objects');
                $adi->appendChild($objects);

                /**
                 * ElementType="Series" start
                 */
                // 单个<Object>
                $object = $dom->createElement('Object');
                // Series-连续剧剧头信息|Program-节目信息|Movie-媒体内容信息|Channel-频道信息|PhysicalChannel-频道物理参数信息|Schedule-频道节目编排信息|Cast-人物信息|Picture-图片信息|Category-编排目录信息, 必须
                // $no3 = $dom->createAttribute('id');//创建属性节点：id
                // $no3->value = 1;//给属性节点赋值
                // $no2->appendChild($no3);//把属性节点加入到book节点中
                $object->setAttribute('ElementType', $elementType);
                // $no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说，非常好看！');//创建文CDATA节点
                // 唯一ID, 必须
                $object->setAttribute('ID', $id);
                // 发布-REGIST, 更新-UPDATE, 删除-DELETE, 必须
                $object->setAttribute('Action', $action);
                // 全局唯一标志, 必须
                $object->setAttribute('Code', $code);
                // 业务类型,非必须
                $object->setAttribute('BizDomain', $bizDomain);
                $objects->appendChild($object);

                // // 第三方上线标识<Property>
                // $property = $dom->createElement('Property');
                // // 第三方上线标识, 非必须
                // $property->setAttribute('Name', 'OnlineIdentify');
                // $text = $dom->createTextNode($onlineIdentify);
                // $property->appendChild($text);
                // $object->appendChild($property);

                // 视频名称<Property>
                $property = $dom->createElement('Property');
                // 视频名称, 必须
                $property->setAttribute('Name', 'Name');
                $text = $dom->createTextNode($name);
                $property->appendChild($text);
                $object->appendChild($property);

                // 节目订购编号<Property>
                $property = $dom->createElement('Property');
                // 节目订购编号, 非必须
                $property->setAttribute('Name', 'OrderNumber');
                $text = $dom->createTextNode($orderNumber);
                $property->appendChild($text);
                $object->appendChild($property);

                // 原名<Property>
                $property = $dom->createElement('Property');
                // 原名, 非必须
                $property->setAttribute('Name', 'OriginalName');
                $text = $dom->createTextNode($originalName);
                $property->appendChild($text);
                $object->appendChild($property);

                // 剧集排序规则<Property>
                $property = $dom->createElement('Property');
                // 剧集排序名称, 0降序, 1升序, 非必须
                $property->setAttribute('Name', 'SortName');
                $text = $dom->createTextNode($sortName);
                $property->appendChild($text);
                $object->appendChild($property);

                // 剧集排序规则<Property>
                $property = $dom->createElement('Property');
                // 剧集排序名称, 0降序, 1升序, 非必须
                $property->setAttribute('Name', 'SearchName');
                $text = $dom->createTextNode($searchName);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 首播日期, 必须
                $property->setAttribute('Name', 'OrgAirDate');
                $text = $dom->createTextNode($orgairDate);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 有效开始时间, 必须
                $property->setAttribute('Name', 'LicensingWindowStart');
                $text = $dom->createTextNode($licensingWindowStart);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 有效结束时间, 必须
                $property->setAttribute('Name', 'LicensingWindowEnd');
                $text = $dom->createTextNode($licensingWindowEnd);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 新到天数, 非必须
                $property->setAttribute('Name', 'DisplayAsNew');
                $text = $dom->createTextNode($displayAsNew);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 剩余天数, 非必须
                $property->setAttribute('Name', 'DisplayLastChance');
                $text = $dom->createTextNode($displayLastChance);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 拷贝保护标志, 必须
                $property->setAttribute('Name', 'Macrovision');
                $text = $dom->createTextNode($macrovision);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 含税定价, 不必须
                $property->setAttribute('Name', 'Price');
                $text = $dom->createTextNode($price);
                $property->appendChild($text);
                $object->appendChild($property);

                // // <Property>
                // $property = $dom->createElement('Property');
                // // 总集数, 必须
                // $property->setAttribute('Name', 'VolumnCount');
                // $text = $dom->createTextNode($volumnCount);
                // $property->appendChild($text);
                // $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目状态标志, 必须
                $property->setAttribute('Name', 'Status');
                $text = $dom->createTextNode($status);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目描述, 必须
                $property->setAttribute('Name', 'Description');
                $text = $dom->createTextNode($description);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目内容类型, 必须
                $property->setAttribute('Name', 'Type');
                $text = $dom->createTextNode($type);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 关键字, 多个关键字直接用;
                $property->setAttribute('Name', 'Keywords');
                $text = $dom->createTextNode($keywords);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 标签集合, 非必须
                $property->setAttribute('Name', 'Tags');
                $text = $dom->createTextNode($tags);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 保留字段, 非必须
                $property->setAttribute('Name', 'Reserve1');
                $text = $dom->createTextNode($reserve1);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 保留字段, 非必须
                $property->setAttribute('Name', 'Reserve2');
                $text = $dom->createTextNode($reserve2);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 保留字段, 非必须
                $property->setAttribute('Name', 'Reserve3');
                $text = $dom->createTextNode($reserve3);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 保留字段, 非必须
                $property->setAttribute('Name', 'Reserve4');
                $text = $dom->createTextNode($reserve4);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 保留字段, 非必须
                $property->setAttribute('Name', 'Reserve5');
                $text = $dom->createTextNode($reserve5);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 关联内容唯一标识, 非必须
                $property->setAttribute('Name', 'RMediaCode');
                $text = $dom->createTextNode($rMediaCode);
                $property->appendChild($text);
                $object->appendChild($property);

                /**
                 * ElementType="Series" end
                 */
                /**
                 * ElementType="Program" start
                 */

                // Program-节目信息
                $elementType = 'Program';
                // 属性名:ID(唯一ID);必填:Y;类型长度:32;
                $program_id = str_pad(makeRandomNumber(32), 32, "0", STR_PAD_LEFT); // uniqid() . udate('YmdHisu')
                // 属性名:Action(操作类型);取值:发布=>REGIST, 更新=>UPDATE, 删除=>DELETE;必填:Y;
                $action = 'REGIST';
                // 属性名:Code(全局唯一标志);必填:Y;类型长度:128;
                $program_code = $program_id;
                // 节点名:Name(连续剧名称);必填:Y;类型长度:128;
                $name = '';
                // 节点名:OrderNumber(节目订购编号);必填:N;类型长度:string,10;
                $orderNumber = '';
                // 节点名:OriginalName(原名);必填:N;类型长度:string,128;
                $originalName = '';
                // 节点名:SortName(索引名称供界面排序);必填:N;类型长度:string,128;
                $sortName = '';
                // 节点名:SearchName(搜索名称供界面搜索);必填:N;类型长度:string,128;
                $searchName = '';
                // 节点名:Genre(Program的默认类别);必填:N;类型长度:string,128;
                $genre = '';
                // 节点名:ActorDisplay(演员列表);必填:N;类型长度:string,1024;
                $actors = '';
                // 节点名:WriterDisplay(演员列表);必填:N;类型长度:string,256;
                $directors = '';
                // 节点名:OriginalCount(国家地区);必填:N;类型长度:string,64;
                $vareas = '';
                // 节点名:Language(语言);必填:Y;类型长度:string,64;
                $vlangs = '';
                // 节点名:ReleaseYear(上映年份);必填:Y;类型长度:string,64;
                $vyears = '';
                // 首播时间
                $orgairDate = '';
                // 节点名:LicensingWindowStart(有效开始时间YYYYMMDDHH24MiSS);必填:Y;类型长度:string,14;
                $licensingWindowStart = '';
                // 节点名:LicensingWindowEnd(有效结束时间YYYYMMDDHH24MiSS);必填:Y;类型长度:string,14;
                $licensingWindowEnd = '';
                // 节点名:DisplayAsNew(新到天数);必填:N;类型长度:int,3;
                $displayAsNew = '';
                // 节点名:DisplayLastChance(剩余天数);必填:N;类型长度:int,3;
                $displayLastChance = '';
                // 节点名:Macrovision(拷贝保护标志);取值:0=>无拷贝保护,1=>有拷贝保护;必填:Y;类型长度:int,1;默认:0;
                $macrovision = 0;
                // 节点名:Description(节目描述);必填:Y;类型长度:string,2048;
                $description = '';
                // 节点名:PriceTaxIn(列表定价);必填:N;类型长度:float,12,2;
                $priceTaxIn = '';
                // 节点名:Status(节目状态标志);必填:N;类型长度:int,1;默认:1;
                $status = 1;
                // 节点名:SourceType;取值:1=>VDO,5=>Advertisment;必填:Y;类型长度:int,1;默认:1;
                $sourceType = '';
                // 节点名:SeriesFlag;取值:0=>普通VDO,1=>连续剧剧集;必填:Y;int,1;
                $seriesFlag = '';
                // 节点名:Type;必填:Y;类型长度:string,128;
                $type = '';
                // 节点名:Keywords(关键字);必填:Y;类型长度:string,1024;多个关键字使用/分隔;
                $keywords = '';
                // 节点名:Tags(标签集合);必填:Y;类型长度:string,1024;多个标签使用/分隔;
                $tags = '';
                // 节点名:Reserve1(保留字段);必填:N;类型长度:string,1024;
                $reserve1 = '';
                // 节点名:Reserve2(保留字段);必填:N;类型长度:string,1024;
                $reserve2 = '';                
                // 节点名:Reserve3(保留字段);必填:N;类型长度:string,256;
                $reserve3 = '';                
                // 节点名:Reserve4(保留字段);必填:N;类型长度:string,256;
                $reserve4 = '';
                // 节点名:Reserve5(保留字段);必填:N;类型长度:string,256;
                $reserve5 = '';
                // 节点名:StrorageType(存储分发策略要求);取值:必填:N;类型长度:string,64;
                $strorageType = '';
                // 节点名:RMediaCode(关联内容唯一标识);必填:N;类型长度:String,128;
                $rMediaCode = '';
                // 节点名:DefinitionFlag(节目清晰度标识);取值:0=>标清,1=>高清,2=>超清,3=>4K,4=>杜比
                $definitionFlag = '';

                // 单个<Object>
                $object = $dom->createElement('Object');
                // Series-连续剧剧头信息|Program-节目信息|Movie-媒体内容信息|Channel-频道信息|PhysicalChannel-频道物理参数信息|Schedule-频道节目编排信息|Cast-人物信息|Picture-图片信息|Category-编排目录信息, 必须
                // $no3 = $dom->createAttribute('id');//创建属性节点：id
                // $no3->value = 1;//给属性节点赋值
                // $no2->appendChild($no3);//把属性节点加入到book节点中
                $object->setAttribute('ElementType', $elementType);
                // $no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说，非常好看！');//创建文CDATA节点
                // 唯一ID, 必须
                $object->setAttribute('ID', $program_id);
                // 发布-REGIST, 更新-UPDATE, 删除-DELETE, 必须
                $object->setAttribute('Action', $action);
                // 全局唯一标志, 必须
                $object->setAttribute('Code', $program_code);
                $objects->appendChild($object);
                // 业务类型,非必须
                $object->setAttribute('BizDomain', $bizDomain);
                $objects->appendChild($object);
                // 视频名称<Property>
                $property = $dom->createElement('Property');
                // 视频名称, 必须
                $property->setAttribute('Name', 'Name');
                $text = $dom->createTextNode($name);
                $property->appendChild($text);
                $object->appendChild($property);

                // 节目订购编号<Property>
                $property = $dom->createElement('Property');
                // 节目订购编号, 非必须
                $property->setAttribute('Name', 'OrderNumber');
                $text = $dom->createTextNode($orderNumber);
                $property->appendChild($text);
                $object->appendChild($property);

                // 原名<Property>
                $property = $dom->createElement('Property');
                // 原名, 非必须
                $property->setAttribute('Name', 'OriginalName');
                $text = $dom->createTextNode($originalName);
                $property->appendChild($text);
                $object->appendChild($property);

                // 剧集排序规则<Property>
                $property = $dom->createElement('Property');
                // 剧集排序规则, 0降序, 1升序, 必须
                $property->setAttribute('Name', 'SortName');
                $text = $dom->createTextNode($sortName);
                $property->appendChild($text);
                $object->appendChild($property);

                // 剧集排序规则<Property>
                $property = $dom->createElement('Property');
                // 剧集排序名称, 0降序, 1升序, 非必须
                $property->setAttribute('Name', 'SearchName');
                $text = $dom->createTextNode($searchName);
                $property->appendChild($text);
                $object->appendChild($property);

                // Program默认类别<Property>
                $property = $dom->createElement('Property');
                // Program默认类别, 非必须
                $property->setAttribute('Name', 'Genre');
                $text = $dom->createTextNode($genre);
                $property->appendChild($text);
                $object->appendChild($property);

                // 演员列表<Property>
                $property = $dom->createElement('Property');
                // 演员列表, 必须
                $property->setAttribute('Name', 'ActorDisplay');
                $text = $dom->createTextNode($actors);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 导演列表, 必须
                $property->setAttribute('Name', 'WriterDisplay');
                $text = $dom->createTextNode($directors);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 国家, 必须
                $property->setAttribute('Name', 'OriginalCountry');
                $text = $dom->createTextNode($vareas);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 语言, 非必须
                $property->setAttribute('Name', 'Language');
                $text = $dom->createTextNode($vlangs);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 上映年份, 必须
                $property->setAttribute('Name', 'ReleaseYear');
                $text = $dom->createTextNode($vyears);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 首播日期, 必须
                $property->setAttribute('Name', 'OrgAirDate');
                $text = $dom->createTextNode($orgairDate);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 有效开始时间, 必须
                $property->setAttribute('Name', 'LicensingWindowStart');
                $text = $dom->createTextNode($licensingWindowStart);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 有效结束时间, 必须
                $property->setAttribute('Name', 'LicensingWindowEnd');
                $text = $dom->createTextNode($licensingWindowEnd);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 新到天数, 非必须
                $property->setAttribute('Name', 'DisplayAsNew');
                $text = $dom->createTextNode($displayAsNew);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 剩余天数, 非必须
                $property->setAttribute('Name', 'DisplayLastChance');
                $text = $dom->createTextNode($displayLastChance);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 拷贝保护标志, 必须
                $property->setAttribute('Name', 'Macrovision');
                $text = $dom->createTextNode($macrovision);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目描述, 必须
                $property->setAttribute('Name', 'Description');
                $text = $dom->createTextNode($description);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 列表定价, 不必须
                $property->setAttribute('Name', 'PriceTaxIn');
                $text = $dom->createTextNode($priceTaxIn);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目状态标志, 必须
                $property->setAttribute('Name', 'Status');
                $text = $dom->createTextNode($status);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // VOD/Adverisement, 必须
                $property->setAttribute('Name', 'SourceType');
                $text = $dom->createTextNode($sourceType);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 普通VOD, 必须
                $property->setAttribute('Name', 'SeriesFlag');
                $text = $dom->createTextNode($seriesFlag);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目内容类型, 必须
                $property->setAttribute('Name', 'Type');
                $text = $dom->createTextNode($type);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 关键字, 多个关键字直接用/
                $property->setAttribute('Name', 'Keywords');
                $text = $dom->createTextNode($keywords);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 标签集合, 必须
                $property->setAttribute('Name', 'Tags');
                $text = $dom->createTextNode($tags);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 保留字段, 非必须
                $property->setAttribute('Name', 'Reserve1');
                $text = $dom->createTextNode($reserve1);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 保留字段, 非必须
                $property->setAttribute('Name', 'Reserve2');
                $text = $dom->createTextNode($reserve2);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 保留字段, 非必须
                $property->setAttribute('Name', 'Reserve3');
                $text = $dom->createTextNode($reserve3);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 保留字段, 非必须
                $property->setAttribute('Name', 'Reserve4');
                $text = $dom->createTextNode($reserve4);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 保留字段, 非必须
                $property->setAttribute('Name', 'Reserve5');
                $text = $dom->createTextNode($reserve5);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目时长, 必须
                $property->setAttribute('Name', 'StrorageType');
                $text = $dom->createTextNode($strorageType);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 关联内容唯一标识, 非必须
                $property->setAttribute('Name', 'RMediaCode');
                $text = $dom->createTextNode($rMediaCode);
                $property->appendChild($text);
                $object->appendChild($property);

                // 节目清晰度标识<Property>
                $property = $dom->createElement('Property');
                // 节目清晰度标识 非必须
                $property->setAttribute('Name', 'DefinitionFlag');
                $text = $dom->createTextNode($definitionFlag);
                $property->appendChild($text);
                $object->appendChild($property);

                /**
                 * ElementType="Program" end
                 */
                /**
                 * ElementType="Channel" start
                 */

                $elementType = 'Channel';
                $vcstreams     = $vchan->vcstreams;
                // 存放文件的id和code
                $channels = [];
                foreach ($vcstreams as $key => $value) {
                    // 属性名:ID(唯一ID);必填:Y;类型长度:32;
                    $channel_id = str_pad(makeRandomNumber(32), 32, "0", STR_PAD_LEFT);
                    $channels[] = $channel_id;
                    // 属性名:Action(操作类型);取值:发布=>REGIST, 更新=>UPDATE, 删除=>DELETE;必填:Y;
                    $channel_action = 'REGIST';
                    // 属性名:Code(全局唯一标志);必填:Y;类型长度:128;
                    $channel_code = $channel_id;
                    // 属性名:ChannelNumber(建议频道号);必填:N;类型长度:int,3;
                    $channelNumber = '';
                    // 属性名:Name(频道名称);必填:Y;类型长度:string,64
                    $name = $value->name;
                    // 属性名:CailSign(台标名称)
                    $vlogo = $vchan->vlogo;
                    // 属性名:TimeShift(时迁标志)
                    $timeshift = $value->timeshift;
                    // 属性名:StorageDuration(存储时长,单位小时);
                    $storageDuration = '';
                    // 属性名:TimeShiftDuration(默认时移时长);
                    $timeShiftDuration = '';
                    // 属性名:Description(描述信息)
                    $description = $value->short_desc;
                    // 属性名:Country(国家);
                    $country = '';
                    // 属性名:State(州/省)
                    $state = '';
                    // 属性名:City(城市)
                    $city = '';
                    // 属性名:Type(频道类型);取值:1=>直播频道;必填:Y;类型长度:int,1;
                    $channel_type = 1;
                    // 属性名:SubType(信号源类型);取值:1=>信号源来自live,2=>信号源来自virtual;必填:Y;类型长度:String,1024;
                    $subType = 1;
                    // 属性名:Language(语言);必填:N;类型长度:string,128;
                    $vlangs = '';
                    // 属性名:Status(状态标志);取值:0=>失效, 1=>生效;必填:Y;类型长度:int,1;默认为:0;
                    $status = 1;
                    // 属性名:StartTime(播放开始时间HH24MI);必填:Y;类型长度:date;
                    $startTime = '';
                    // 属性名:EndTime(播放结束时间HH24MI);必填:Y;类型长度:date;
                    $endTime = '';
                    // 属性名:Macrovision(拷贝保护标志);取值:0=>无拷贝保护, 1=>有拷贝保护;必填:N;类型长度:int,1;
                    $macrovision = '';
                    // 属性名:VideoType(视频参数);必填:N;类型长度:string,10;
                    $videoType = '';
                    // 属性名:AudioType(音频参数);必填:N;类型长度:string,10;
                    $audioType = '';
                    // 属性名:StreamType(码流标志);必填:N;类型长度:string,16;单位为byte;
                    $streamType = '';
                    // 属性名:Bilingual(双语标志(0/1));必填:N;类型长度:int,1;
                    $bilingual = '';
                    // 属性名:URL(Web频道入口地址);必填:N;类型长度:string,128;当type=2时,这个属性必填;
                    $channelUrl = '';

                    // 单个<Object>
                    $object = $dom->createElement('Object');
                    // Series-连续剧剧头信息|Program-节目信息|Movie-媒体内容信息|Channel-频道信息|PhysicalChannel-频道物理参数信息|Schedule-频道节目编排信息|Cast-人物信息|Picture-图片信息|Category-编排目录信息, 必须
                    // $no3 = $dom->createAttribute('id');//创建属性节点：id
                    // $no3->value = 1;//给属性节点赋值
                    // $no2->appendChild($no3);//把属性节点加入到book节点中
                    $object->setAttribute('ElementType', $elementType);
                    // $no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说，非常好看！');//创建文CDATA节点
                    // 唯一ID, 必须
                    $object->setAttribute('ID', $channel_id);
                    // 发布-REGIST, 更新-UPDATE, 删除-DELETE, 必须
                    $object->setAttribute('Action', $channel_action);
                    // 全局唯一标志, 必须
                    $object->setAttribute('Code', $channel_code);
                    $objects->appendChild($object);
                    // 业务类型,非必须
                    $object->setAttribute('BizDomain', $bizDomain);
                    $objects->appendChild($object);
                    // 建议频道号<Property>
                    $property = $dom->createElement('Property');
                    // 建议频道号, 必须
                    $property->setAttribute('Name', 'ChannelNumber');
                    $text = $dom->createTextNode($channelNumber);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 频道名称<Property>
                    $property = $dom->createElement('Property');
                    // 频道名称, 必须
                    $property->setAttribute('Name', 'Name');
                    $text = $dom->createTextNode($name);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 台标名称<Property>
                    $property = $dom->createElement('Property');
                    // 台标名称, 必须
                    $property->setAttribute('Name', 'CailSign');
                    $text = $dom->createTextNode($vlogo);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 时迁标志<Property>
                    $property = $dom->createElement('Property');
                    // 时迁标志, 非必须
                    $property->setAttribute('Name', 'TimeShift');
                    $text = $dom->createTextNode($timeshift);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 存储时长<Property>
                    $property = $dom->createElement('Property');
                    // 存储时长, 非必须
                    $property->setAttribute('Name', 'StorageDuration');
                    $text = $dom->createTextNode($storageDuration);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 默认时移时长<Property>
                    $property = $dom->createElement('Property');
                    // 默认时移时长, 非必须
                    $property->setAttribute('Name', 'TimeShiftDuration');
                    $text = $dom->createTextNode($timeShiftDuration);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // ScreenFormat<Property>
                    $property = $dom->createElement('Property');
                    // ScreenFormat, 非必须
                    $property->setAttribute('Name', 'ScreenFormat');
                    $text = $dom->createTextNode($screenFormat);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 描述信息<Property>
                    $property = $dom->createElement('Property');
                    // 描述信息, 非必须
                    $property->setAttribute('Name', 'Description');
                    $text = $dom->createTextNode($description);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 国家<Property>
                    $property = $dom->createElement('Property');
                    // 国家, 非必须
                    $property->setAttribute('Name', 'Country');
                    $text = $dom->createTextNode($country);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 州/省<Property>
                    $property = $dom->createElement('Property');
                    // 州/省, 非必须
                    $property->setAttribute('Name', 'State');
                    $text = $dom->createTextNode($state);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 城市<Property>
                    $property = $dom->createElement('Property');
                    // 城市, 非必须
                    $property->setAttribute('Name', 'City');
                    $text = $dom->createTextNode($city);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 频道类型<Property>
                    $property = $dom->createElement('Property');
                    // 频道类型, 非必须
                    $property->setAttribute('Name', 'Type');
                    $text = $dom->createTextNode($channel_type);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 信号源类型<Property>
                    $property = $dom->createElement('Property');
                    // 信号源类型, 必须
                    $property->setAttribute('Name', 'SubType');
                    $text = $dom->createTextNode($subType);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 语言<Property>
                    $property = $dom->createElement('Property');
                    // 语言, 必须
                    $property->setAttribute('Name', 'Language');
                    $text = $dom->createTextNode($vlangs);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 状态标志<Property>
                    $property = $dom->createElement('Property');
                    // 状态标志, 非必须
                    $property->setAttribute('Name', 'Status');
                    $text = $dom->createTextNode($status);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 播放开始时间HH24MI<Property>
                    $property = $dom->createElement('Property');
                    // 播放开始时间HH24MI, 非必须
                    $property->setAttribute('Name', 'StartTime');
                    $text = $dom->createTextNode($startTime);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 播放结束时间HH24MI<Property>
                    $property = $dom->createElement('Property');
                    // 播放结束时间HH24MI, 必须
                    $property->setAttribute('Name', 'EndTime');
                    $text = $dom->createTextNode($endTime);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 拷贝保护标志<Property>
                    $property = $dom->createElement('Property');
                    // 拷贝保护标志, 必须
                    $property->setAttribute('Name', 'Macrovision');
                    $text = $dom->createTextNode($macrovision);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 码流标志<Property>
                    $property = $dom->createElement('Property');
                    // 码流标志, 必须
                    $property->setAttribute('Name', 'StreamType');
                    $text = $dom->createTextNode($streamType);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 视频参数<Property>
                    $property = $dom->createElement('Property');
                    // 视频参数, 必须
                    $property->setAttribute('Name', 'VideoType');
                    $text = $dom->createTextNode($videoType);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 音频参数<Property>
                    $property = $dom->createElement('Property');
                    // 音频参数, 必须
                    $property->setAttribute('Name', 'AudioType');
                    $text = $dom->createTextNode($audioType);
                    $property->appendChild($text);
                    $object->appendChild($property);
                    
                    // 双语标志<Property>
                    $property = $dom->createElement('Property');
                    // 双语标志, 必须
                    $property->setAttribute('Name', 'Bilingual');
                    $text = $dom->createTextNode($bilingual);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // Web频道入口地址<Property>
                    $property = $dom->createElement('Property');
                    // Web频道入口地址, 必须
                    $property->setAttribute('Name', 'URL');
                    $text = $dom->createTextNode($channelUrl);
                    $property->appendChild($text);
                    $object->appendChild($property);
                }

                /**
                 * ElementType="Channel" end
                 */
                
                /**
                 * ElementType="Picture" start
                 */
                $elementType = 'Picture';
                $vcposteris   = $vchan->vcposteris;
                // 存放图片的id和code
                $pictures = [];
                foreach ($vcposteris as $key => $value) {
                    // 属性名:ID(唯一ID);必填:Y;类型长度:32;
                    $picture_id = str_pad(makeRandomNumber(32), 32, "0", STR_PAD_LEFT);
                    $pictures[] = $picture_id;
                    // 属性名:Action(操作类型);取值:发布=>REGIST, 更新=>UPDATE, 删除=>DELETE;必填:Y;
                    $picture_action = 'REGIST';
                    // 属性名:Code(全局唯一标志);必填:Y;类型长度:128;
                    $picture_code = $picture_id;
                    // 属性名:FileURL(图片文件URL);类型长度:string,1024;此路径支持ftp、http两种格式
                    $picture_href = $value->cposter_url;
                    // 属性名:Description(描述);类型长度:string,1024;
                    $picture_description = $value->name;
                    // 属性名:Type(类型);取值:0=>大图, 1=>中图, 2=>小图, 3=>竖图, 4=>横图, 5=>方图, 6=>背景图, 7=>栏目图片
                    // $picture_type = ($value->imgtpl_id - 1);
                    // 属性名:Description(描述);类型长度:string,1024;
                    $description=$value->short_desc;

                    // 单个<Object>
                    $object = $dom->createElement('Object');
                    // Series-连续剧剧头信息|Program-节目信息|Movie-媒体内容信息|Channel-频道信息|PhysicalChannel-频道物理参数信息|Schedule-频道节目编排信息|Cast-人物信息|Picture-图片信息|Category-编排目录信息, 必须
                    // $no3 = $dom->createAttribute('id');//创建属性节点：id
                    // $no3->value = 1;//给属性节点赋值
                    // $no2->appendChild($no3);//把属性节点加入到book节点中
                    $object->setAttribute('ElementType', $elementType);
                    // $no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说，非常好看！');//创建文CDATA节点
                    // 唯一ID, 必须
                    $object->setAttribute('ID', $picture_id);
                    // 发布-REGIST, 更新-UPDATE, 删除-DELETE, 必须
                    $object->setAttribute('Action', $picture_action);
                    // 全局唯一标志, 必须
                    $object->setAttribute('Code', $picture_code);
                    $objects->appendChild($object);

                    // 图片文件URL<Property>
                    $property = $dom->createElement('Property');
                    // 图片文件URL
                    $property->setAttribute('Name', 'FileURL');
                    $real_picture_href =  'ftp://' . $ftp_config['username'] . ':' . $ftp_config['password'] . '@' . $ftp_config['host'] . '/' . $picture_href;
                    $text = $dom->createTextNode($real_picture_href);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 描述<Property>
                    $property = $dom->createElement('Property');
                    // 描述
                    $property->setAttribute('Name', 'Description');
                    $text = $dom->createTextNode($description);
                    $property->appendChild($text);
                    $object->appendChild($property);

                }
                /**
                 * ElementType="Picture" end
                 */

                /**
                 * ElementType="Mappings" start
                 */
                // 建立<Mappings>元素并将其作为<ADI>的子元素
                $mappings = $dom->createElement('Mappings');
                $adi->appendChild($mappings);

                // 单个<Object>
                $mapping = $dom->createElement('Mapping');
                // 遍历图片信息
                foreach ($pictures as $value) {
                    $mapping = $dom->createElement('Mapping');
                    $mappings->appendChild($mapping);
                    $elementType = 'Picture';
                    $partentType = 'Series';
                    $action = 'REGIST';
                    $type = 0;
                    $mapping->setAttribute('ParentType', $partentType);
                    // $no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说，非常好看！');//创建文CDATA节点
                    // 唯一ID, 必须
                    $mapping->setAttribute('ParentID', $value);
                    $mapping->setAttribute('ElementType', $elementType);
                    $mapping->setAttribute('ElementID', $id);
                    // 全局唯一标志, 必须
                    $mapping->setAttribute('ParentCode', $value);
                    $mapping->setAttribute('ElementCode', $code);
                    // 发布-REGIST, 更新-UPDATE, 删除-DELETE, 必须
                    $mapping->setAttribute('Action', $action);

                    // <Property>
                    $property = $dom->createElement('Property');
                    // Sequence
                    $property->setAttribute('Name', 'Sequence');
                    $text = $dom->createTextNode(90);
                    $property->appendChild($text);
                    $mapping->appendChild($property);

                    // 图片类型<Property>
                    $property = $dom->createElement('Property');
                    // 图片类型, 必须
                    $property->setAttribute('Name', 'Type');
                    $text = $dom->createTextNode($type);
                    $property->appendChild($text);
                    $mapping->appendChild($property);
                }
                /**
                 * ElementType="Mappings" end
                 */
                
                // echo $dom->saveXML();
                // die();
                // 保存到xml
                // 完整路径
                $fullPath1 = $fullPath . 'vchans' . $vchan_id . '.xml';
                $bool      = Storage::disk($driver)->put($fullPath1, $dom->saveXML());
                if ($bool) {
                    /**
                     * 发起注入请求start
                     */
                    /**
                     *
                     * 内容下发
                     * 消息方向:CSP->LSP, LSP以 `cmspost` 接受XML指令
                     * 通知响应: Result:0成功, -1 失败; ErrorDescription:错误信息详细描述
                     * 下游接受消息, 并同步反馈接受结果
                     */
                    $time   = udate('YmdHisu');
                    // cpid
                    // $cp_id    = $config_media_injection_cp_id;
                    // CSPID
                    $csp_id = config('custom.base.media_injection.csp_id');
                    // LSPID
                    $lsp_id = config('custom.base.media_injection.lsp_id');
                    // 注入调用的地址
                    $url = $config_media_injection_url;
                    // xmld地址
                    // $file_url = 'ftp://web1:123456@10.69.42.148/upload/xml/3.xml';
                    $file_url = 'ftp://' . $ftp_config['username'] . ':' . $ftp_config['password'] . '@' . $ftp_config['host']. '/' . $fullPath1;

                    // 定义content-type为xml
                    // $header[] = "Content-Type: application/soap+xml";
                    $arr_header[] = "Content-type: text/xml";
                    $arr_header[] = "SOAPAction:\"\"";
                    // 构造xml
                    /*$post_data = '<?xml version="1.0" encoding="UTF-8"?>';*/
                    // $data = [];
                    // $post_data = '';
                    $post_data = '<?xml version="1.0" encoding="UTF-8"?>';
                    $post_data .= '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
                    $post_data .= '<soapenv:Body>';
                    $post_data .= '<ns1:ExecCmd xmlns:ns1="iptv" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">';
                    $post_data .= '<CSPID xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' . $csp_id . '</CSPID>';
                    $post_data .= '<LSPID xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' . $lsp_id . '</LSPID>';
                    $post_data .= '<CorrelateID xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' . $msg_id . '</CorrelateID>';
                    $post_data .= '<CmdFileURL xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' . $file_url . '</CmdFileURL>';
                    $post_data .= '</ns1:ExecCmd>';
                    $post_data .= '</soapenv:Body>';
                    $post_data .= '</soapenv:Envelope>';
                    // $data['cmspost'] = $post_data;
                    // 首先检测是否支持curl
                    if (!extension_loaded("curl")) {
                        trigger_error("对不起，请开启curl功能模块！", E_USER_ERROR);
                    }

                    // 初始一个curl会话
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_header);
                    // 设置url
                    curl_setopt($ch, CURLOPT_URL, $url);
                    // TRUE, 将curl_exec()获取的信息以字符串返回, 而不是直接输出
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    // 超时时间
                    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
                    // 设置发送方式:post
                    curl_setopt($ch, CURLOPT_POST, 1);
                    // 设置发送数据
                    // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
                    // 执行cURL会话
                    $response = curl_exec($ch);
                    if (curl_errno($ch)) {
                        print curl_error($ch);
                    }
                    curl_close($ch);
                    // post的数据为xml字符串，通过 $xml = simplexml_load_string($post_data);转换成xml对象
                    $response_xml = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
                    // 把simplexml对象转换成 json，再将 json 转换成数组。
                    $response_array = $value_array = json_decode(json_encode($response_xml), true);
                    if ($response_array && $response_array['@attributes'] && $response_array['@attributes']['ret'] === '0') {
                        Vchan::where('id', '=', $vchan_id)->update(['msg_id' => $msg_id, 'sync_result' => 1]);
                    }

                    /**
                     * 发起注入请求end
                     */
                    // 更新注入状态为正在注入
                    Vchan::where('id', '=', $vchan->id)->update(['async_result' => 0]);
                    // 注入媒资总数
                    $injectionVchancount ++;
                } else {
                    // 错误的媒资id
                    $errorRow[] = $vchan_id;
                }
            }
         });
        
        // 结束时间
        $endtime        = microtime(true);
        $consuming_time = round($endtime - $starttime, 4);
        $endMemory      = memory_get_usage();
        $usedMemory     = ($endMemory - $startMemory) / 1024;

        $flag    = true;
        $errorCount = count($errorRow);
        $message = '<div style="word-wrap: break-word;">';
        $message .= '耗时:';
        $message .= $consuming_time . '秒<br />';
        $message .= '耗费内存:';
        $message .= "{$usedMemory} K<br />";
        $message .= '注入总数:';
        $message .= $this->injectVchaniHistory->count . '<br />';
        $message .= '注入成功数:';
        $message .= $injectionVchancount . '<br />';
        $message .= '注入失败数:';
        $message .= count($errorRow) . '<br />';
        if ($errorCount > 0) {
            $flag = false;
            $message .= '注入失败的媒资id:' . implode(',', $errorRow) . '<br />';
        }
        $message .= '</div>';
        //通过标志位判断前端返回时成功还是失败
        if ($flag) {
            $message = '<div class="alert alert-success">' . $message . '</div>';
        } else {
            $message = '<div class="alert alert-error">' . $message . '</div>';
        }

        $this->injectVchanHistory->consuming_time = $consuming_time;
        $this->injectVchanHistory->used_memory = $usedMemory;
        $this->injectVchanHistory->content = $message;
        $this->injectVchanHistory->save();
    }
}
